<template>
  <div>
     <el-row :gutter="10" class="mb8" v-if="isEdit">
          <el-col>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              style="float: right;"
              @click="handleAdd"
              v-hasPermi="['labbase:receipt_form:edit']"
            >新增</el-button>
          </el-col>
    </el-row>
    <el-table v-loading="loading" :data="receiptDetailsList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="资产名称" align="center" prop="assetName" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="单价(元)" align="center" prop="unitPrice" />
      <el-table-column label="规格" align="center" prop="specs" />
      <el-table-column label="小计" align="center" prop="totalMoney" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isEdit && isPermit('labbase:receipt_form:edit')">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row, scope.$index+1)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center;" v-if="receiptForm.totalPrice">金额总计:{{receiptForm.totalPrice}};(大写){{receiptForm.originalVoucher}}</div>
    <!-- 添加或修改receiptDetails对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产名称" prop="assetName">
          <el-input v-model="form.assetName" placeholder="请输入资产名称" class="input_w"/>
        </el-form-item>
        <el-form-item label="规格" prop="specs">
          <el-input v-model="form.specs" placeholder="请输入规格" class="input_w"/>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" type="number" placeholder="请输入数量" class="input_w" @blur="getMony"
          @keyup.enter.native="getMony"/>
        </el-form-item>
        <el-form-item label="单价(元)" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入税后单价(元)" class="input_w" @blur="getMony"
          @keyup.enter.native="getMony"/>
        </el-form-item>
        <el-form-item label="小计" prop="totalMoney">
          <el-input v-model="form.totalMoney" disabled class="input_w"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getSumMoney} from "@/api/common.js"
import { listReceiptDetails, getReceiptDetails, delReceiptDetails, addReceiptDetails, updateReceiptDetails } from "@/api/labbase/receiptDetails";

export default {
  name: "ReceiptDetails",
  data() {
    return {
      // 遮罩层
      receiptForm : {},
      loading: true,
      // receiptDetails表格数据
      receiptDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        instanceId: null,
        assetName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetName: [
          { required: true, message: "资产名称不能为空", trigger: "blur" }
        ],
       /* specs: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ], */
        unitPrice: [
          { required: true, message: "单价(元)不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  props:{
    isEdit:Boolean,
    instanceId:Number,
  },
  watch: {
    instanceId: function(val) {
      this.getList();
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询receiptDetails列表 */
    getList() {
      if(!this.instanceId){
        return;
      }
      this.loading = true;
      this.queryParams.instanceId = this.instanceId;
      listReceiptDetails(this.queryParams).then(response => {
        this.receiptForm = response.data;
        this.receiptDetailsList = this.receiptForm.detailsList;
        this.loading = false;
      });
    },
    getMony(){
      if(!this.form.quantity || !this.form.unitPrice){
        this.form.totalMoney = '';
      }else{
        getSumMoney(this.form.quantity,this.form.unitPrice).then(response=>{
          this.form.totalMoney = response.data;
          this.$forceUpdate();
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: null,
        instanceId: null,
        assetName: null,
        specs: null,
        unitPrice: null,
        quantity: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const detailId = row.detailId || this.ids
      getReceiptDetails(detailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.instanceId = this.instanceId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailId != null) {
            updateReceiptDetails(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.receiptForm.totalPrice = response.data.totalPrice;
              this.receiptForm.originalVoucher = response.data.originalVoucher;
              this.receiptDetailsList = response.data.detailsList;
            });
          } else {
            addReceiptDetails(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.receiptForm.totalPrice = response.data.totalPrice;
              this.receiptForm.originalVoucher = response.data.originalVoucher;
              this.receiptDetailsList = response.data.detailsList;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      const detailIds = row.detailId || this.ids;
      this.$confirm('是否确认删除序号为"' + index + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReceiptDetails(detailIds);
        }).then(response => {
          this.receiptForm.totalPrice = response.data.totalPrice;
          this.receiptForm.originalVoucher = response.data.originalVoucher;
          this.receiptDetailsList = response.data.detailsList;
          this.msgSuccess("删除成功");
        })
    },
    isPermit(permitKey){
      if(this.$store.state.user.name=='admin'){
        return true;
      }
      let  permissions = this.$store.getters.permissions;
      return permissions.includes(permitKey);
    },
  }
};
</script>
<style scoped lang="scss">
  .input_w{
    width: 260px;
  }
</style>
